package ua.epam.muzalevskiy.traction.controllers;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

/**
 * IPFilter logs the remote host address from which the request 
 * and requested by the client url.
 *
 * @author Muzalevskiy Vitaliy
 */
public class LogFilter implements Filter{

   private static Logger logger;

   @Override
    public void init(FilterConfig config) throws ServletException{
       logger = Logger.getLogger(LogFilter.class);
    }   

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp,
                         FilterChain chain) throws ServletException, IOException{
        HttpServletRequest httpreq=null;
        if (req instanceof HttpServletRequest){
        httpreq = (HttpServletRequest)req;
            logger.info("Request received from "+ req.getRemoteHost()+" for: "+
                httpreq.getRequestURI());}
        chain.doFilter(req, resp);
    }
    
    @Override
    public void destroy(){
    }
}
